﻿using Common;
using Model;
using POS;
using POS.Framework;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace Cofco.login
{
    public partial class frmLockScreen : DevExpress.XtraEditors.XtraForm
    {
        frmMain frmMain;

        public frmLockScreen(frmMain main)
        {
            InitializeComponent();
            frmMain = main;
            frmMain.IsLockScreen = true;
            string path = Directory.GetCurrentDirectory() + "\\LockImg";
            string[] fileNames = Directory.GetFiles(path);
            foreach (string file in fileNames)
                girls.Add(Image.FromFile(file));
        }

        private void btnUnlock_ButtonPressed(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (MD5.FunEncrypt(btnUnlock.Text.Trim()).ToLower() == MemberInfo.User.Password.ToLower())
            {
                frmMain.IsLockScreen = false;
                this.Close();
            }
            else
            {
                MsgBox.ShowTips("解锁密码错误！");
                btnUnlock.Text = string.Empty;
                btnUnlock.Select();
            }
        }

        private void btnUnlock_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) btnUnlock_ButtonPressed(sender, null);
        }

        private List<Image> girls = new List<Image>();
        private int index = 0;
        private void timerImg_Tick(object sender, EventArgs e)
        {
            if (girls.Count == 0)
            { return; }
            Image currentGirl = girls[index++];
            pic.Image = currentGirl;
            AnimateWindow(pic.Handle, 1000,
                GetRandomAnimationType());
            AnimateWindow(pic.Handle, 1000, AnimationType.AW_CENTER);
            if (index >= girls.Count)
            {
                index = 0;
            }
        }

        private Random random = new Random();
        private AnimationType[] animationTypes = null;
        private AnimationType GetRandomAnimationType()
        {
            if (animationTypes == null)
            {
                animationTypes = Enum.GetValues(typeof(AnimationType))
                    as AnimationType[];
            }
            return animationTypes[random.Next(0, animationTypes.Length - 1)];
        }

        [Flags]
        public enum AnimationType
        {
            AW_HOR_POSITIVE = 0x0001,//从左向右显示
            AW_HOR_NEGATIVE = 0x0002,//从右向左显示
            AW_VER_POSITIVE = 0x0004,//从上到下显示
            AW_VER_NEGATIVE = 0x0008,//从下到上显示
            AW_CENTER = 0x0010,//从中间向四周
            AW_HIDE = 0x10000,
            AW_ACTIVATE = 0x20000,//普通显示
            AW_SLIDE = 0x40000,
            AW_BLEND = 0x80000,//透明渐变显示效果
        }

        [DllImport("user32.dll")]
        public static extern bool AnimateWindow(IntPtr hwnd, uint dwTime, AnimationType dwFlags);
    }
}
